import { getUserInfo } from "@/apis/sysUser"

export default {
    namespaced: true,
    state: {
        token: '',
        userInfo: {}
    },
    getters: {
        isLogin(state) {
            return !!state.token || !!localStorage.getItem("Authorization")
        }
    },
    mutations: {
        setToken(state, token) {
            localStorage.setItem("Authorization", token)
            state.token = token
        },
        clearToken(state) {
            state.token = ''
            localStorage.removeItem("Authorization")
        },
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo || {}
        },
        clearUserInfo(state) {
            state.userInfo = {}
        }
    },
    actions: {
        refreshInfo({ commit }) {
            getUserInfo().then((res) => {
                commit('setUserInfo', res.data)
            })
        }
    }
}